package com.bocloud.cmp.driver.providers.impl;

import org.springframework.stereotype.Service;

import com.bocloud.cmp.driver.config.Configuration;
import com.bocloud.cmp.driver.providers.SnapshotDriver;
import com.bocloud.cmp.driver.providers.ali.AliSnapshotDriver;
import com.bocloud.cmp.driver.providers.intf.SnapshotProvider;
import com.bocloud.cmp.driver.providers.jd.JdSnapshotDriver;
import com.bocloud.cmp.driver.providers.os.OSSnapshotDriver;
import com.bocloud.cmp.driver.providers.vmware.VmwareSnapshotDriver;
import com.bocloud.cmp.driver.utils.VendorConverter;
import com.bocloud.cmp.enums.VendorType;
import com.bocloud.cmp.model.Butler;
import com.bocloud.cmp.model.SnapshotModel;
import com.bocloud.common.model.BsmResult;

/**
 * 快照操作服务接口实现类
 * 
 * @author weiwei
 *
 */
@Service("snapshotProvider")
public class SnapshotProviderImpl implements SnapshotProvider {

	@Override
	public BsmResult list(String vendor, String region) {
		SnapshotDriver driver = null;
		Butler butler = Configuration.instance().getConfig(vendor);
		VendorType type = VendorConverter.convert(butler.getCategory());
		switch (type) {
		case JDYUN:
			driver = new JdSnapshotDriver(butler.getAccessKey(), butler.getSecretKey(), butler.getUrl(), region);
			break;
		case ALIYUN:
			driver = new AliSnapshotDriver(butler.getAccessKey(), butler.getSecretKey(), region);
			break;
		case OPENSTACK:
			driver = new OSSnapshotDriver(butler.getUrl(), butler.getUsername(), butler.getPassword(),
					butler.getTenant(), butler.getDomainName(), null, null);
			break;
		case VMWARE:
			driver = new VmwareSnapshotDriver(butler.getUrl(), butler.getUsername(), butler.getPassword());
			break;
		default:
			break;
		}
		if (null == driver) {
			return new BsmResult(false, "暂不支持！");
		} else if (!driver.accessable()) {
			return new BsmResult(false, "用户鉴权失败！");
		} else {
			return driver.list();
		}
	}

	@Override
	public BsmResult detail(String vendor, String region, SnapshotModel model) {
		SnapshotDriver driver = null;
		Butler butler = Configuration.instance().getConfig(vendor);
		VendorType type = VendorConverter.convert(butler.getCategory());
		switch (type) {
		case JDYUN:
			driver = new JdSnapshotDriver(butler.getAccessKey(), butler.getSecretKey(), butler.getUrl(), region);
			break;
		case ALIYUN:
			driver = new AliSnapshotDriver(butler.getAccessKey(), butler.getSecretKey(), region);
			break;
		case OPENSTACK:
			driver = new OSSnapshotDriver(butler.getUrl(), butler.getUsername(), butler.getPassword(),
					butler.getTenant(), butler.getDomainName(), null, null);
			break;
		case VMWARE:
			break;
		default:
			break;
		}
		if (null == driver) {
			return new BsmResult(false, "暂不支持！");
		} else if (!driver.accessable()) {
			return new BsmResult(false, "用户鉴权失败！");
		} else {
			return driver.detail(model);
		}
	}

	@Override
	public BsmResult remove(String vendor, String region, SnapshotModel model) {
		Butler butler = Configuration.instance().getConfig(vendor);
		SnapshotDriver driver = null;
		VendorType type = VendorConverter.convert(butler.getCategory());
		switch (type) {
		case JDYUN:
			driver = new JdSnapshotDriver(butler.getAccessKey(), butler.getSecretKey(), butler.getUrl(), region);
			break;
		case ALIYUN:
			driver = new AliSnapshotDriver(butler.getAccessKey(), butler.getSecretKey(), region);
			break;
		case OPENSTACK:
			driver = new OSSnapshotDriver(butler.getUrl(), butler.getUsername(), butler.getPassword(),
					butler.getTenant(), butler.getDomainName(), null, null);
			break;
		case VMWARE:
			driver = new VmwareSnapshotDriver(butler.getUrl(), butler.getUsername(), butler.getPassword());
			break;
		default:
			break;
		}
		if (null == driver) {
			return new BsmResult(false, "暂不支持！");
		} else if (!driver.accessable()) {
			return new BsmResult(false, "用户鉴权失败！");
		} else {
			return driver.remove(model);
		}
	}

	@Override
	public BsmResult revert(String vendor, String region, SnapshotModel model) {
		Butler butler = Configuration.instance().getConfig(vendor);
		SnapshotDriver driver = null;
		VendorType type = VendorConverter.convert(butler.getCategory());
		switch (type) {
		case JDYUN:
			driver = new JdSnapshotDriver(butler.getAccessKey(), butler.getSecretKey(), butler.getUrl(), region);
			break;
		case ALIYUN:
			driver = new AliSnapshotDriver(butler.getAccessKey(), butler.getSecretKey(), region);
			break;
		case OPENSTACK:
			driver = new OSSnapshotDriver(butler.getUrl(), butler.getUsername(), butler.getPassword(),
					butler.getTenant(), butler.getDomainName(), null, null);
			break;
		case VMWARE:
			driver = new VmwareSnapshotDriver(butler.getUrl(), butler.getUsername(), butler.getPassword());
			break;
		default:
			break;
		}
		if (null == driver) {
			return new BsmResult(false, "暂不支持！");
		} else if (!driver.accessable()) {
			return new BsmResult(false, "用户鉴权失败！");
		} else {
			return driver.revert(model);
		}
	}

}
